package CreatePhase;

import Entities.Scene;
import World.Application;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class CreatePhaseController{
    private CreatePhasePanel createPhasePanel;
    private Application application;
    private Scene scene;
    private boolean brickPulsado;
    private CreatePhaseRenderer renderer;
    
    public CreatePhaseController(Application application){
        this.application = application;
        createPhasePanel = new CreatePhasePanel();
        createPhasePanel.setSize(new Dimension(500, 500));
        createPhasePanel.setMaximumSize(new Dimension(500, 500));
        createPhasePanel.setMinimumSize(new Dimension(500, 500));
        createPhasePanel.setPreferredSize(new Dimension(500, 500));
        createPhasePanel.setVisible(true);
        scene = new Scene();
    }
    
    public void execute(){
        createPhasePanel.getExitButton().addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                renderer.setStop(true);
                application.remove(createPhasePanel);
                application.returnMainMenu();
            }
        });
        
        createPhasePanel.getBrickButton().addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if(!brickPulsado){
                    System.out.println("True");
                    brickPulsado = true;
                }
                else{
                    System.out.println("False");
                    brickPulsado = false;
                }
            }
        });
        
        createPhasePanel.getFinishButton().addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                //Guardar la escena en el servidor
            }
        });
        
        createPhasePanel.getCanvas().addMouseListener(new MouseListener() {

            @Override
            public void mouseClicked(MouseEvent e) {
                if(brickPulsado){
                    System.out.println("Comprobando colision");
                    try{
                        CollisionControl collisionControl = new CollisionControl(scene, Integer.valueOf(createPhasePanel.getResistanceText().getText()));
                        if(!collisionControl.collision(e.getPoint())){
                            scene.addChild(collisionControl.getBrick());
                            System.out.println("Brick añadido");
                        }
                        else    System.out.println("Brick no añadido");
                    }catch(NumberFormatException ex){
                        System.out.println("No es un entero");
                    }
                }
                
                brickPulsado = false;
            }

            @Override
            public void mousePressed(MouseEvent e) {
                
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                
            }

            @Override
            public void mouseExited(MouseEvent e) {
                
            }
        });
        
        application.getPrincipalPanel().setVisible(false);
        application.add(createPhasePanel);
        renderer = new CreatePhaseRenderer(createPhasePanel.getCanvas(), scene);
        renderer.start();
    }
}
